package com.jzo2o.orders.manager.hander;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.jzo2o.api.customer.AddressBookApi;
import com.jzo2o.api.customer.dto.response.AddressBookResDTO;
import com.jzo2o.api.foundations.ServeApi;
import com.jzo2o.api.foundations.dto.response.ServeAggregationResDTO;
import com.jzo2o.common.expcetions.CommonException;
import com.jzo2o.mvc.utils.UserContext;
import com.jzo2o.orders.base.enums.OrderStatusEnum;
import com.jzo2o.orders.base.model.domain.Orders;
import com.jzo2o.orders.manager.model.dto.request.PlaceOrderReqDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;

import java.time.LocalDate;

/**
 * @Author: lzj
 * 填充订单信息
 */
@Slf4j
public class FillOrderHandler extends OrderHandler {

    private ServeApi serveApi;
    private RedisTemplate redisTemplate;
    private AddressBookApi addressBookApi;

    //订单编号规则
    private String orderIdRule = "%02d%08d%02d%03d%02d";

    public FillOrderHandler() {
        this.serveApi = SpringUtil.getBean(ServeApi.class);
        this.redisTemplate = SpringUtil.getBean(RedisTemplate.class);
        this.addressBookApi = SpringUtil.getBean(AddressBookApi.class);
    }

    @Override
    public void process(Orders orders, PlaceOrderReqDTO reqDTO) {
        log.info("填充订单信息");
        //获取服务信息
        ServeAggregationResDTO serve = serveApi.findById(reqDTO.getServeId());
        if (serve == null) {
            throw new CommonException("服务不存在");
        }
        BeanUtils.copyProperties(serve, orders);
        //订单号
        orders.setId(getOrderId());
        //订单信息
        orders.setUserId(UserContext.currentUserId());
        orders.setServeId(serve.getId());
        orders.setOrdersStatus(OrderStatusEnum.NO_PAY.getStatus());
        orders.setPayStatus(OrderStatusEnum.NO_PAY.getStatus());
        orders.setPurNum(reqDTO.getPurNum());
        orders.setPrice(serve.getPrice());
        orders.setServeStartTime(reqDTO.getServeStartTime());


        //地址信息
        AddressBookResDTO address = addressBookApi.getAddressById(reqDTO.getAddressBookId());
        if (address == null) {
            throw new CommonException("地址信息不存在");
        }

        orders.setServeAddress(address.getProvince() + address.getAddress()
                + address.getCity() + address.getCounty());
        orders.setContactsPhone(address.getPhone());
        orders.setContactsName(address.getName());
        orders.setLocation(address.getLon() + "," + address.getLat());


        orderHandler.process(orders, reqDTO);
    }

    /**
     * 获取订单编号
     * 17位：2位年+8位序号+2位月+3位随机数+2位日
     * 序号每月重置！！！
     * 例如：2400000002095552419
     * 实现方案：
     * 序号通过Redis的INCR 命令实现。
     *
     * @return
     */
    private Long getOrderId() {
        LocalDate now = LocalDate.now();
        String year = now.getYear() + "";
        year = year.substring(2);
        int month = now.getMonthValue();
        int day = now.getDayOfMonth();
        // 获取序号
        Long increment = redisTemplate.opsForValue().increment("order:" + year + month);
        //3位随机数
        int random = RandomUtil.randomInt(100, 999);
        //17位拼接
        String orderId = String.format(orderIdRule, Integer.parseInt(year), increment, month, random, day);
        return Long.parseLong(orderId);
    }
}
